package org.mclll.xwangplugin.town.command.executor;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownApplyDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownApplyManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.ApplyStatus;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;

import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;
import static org.mclll.xwangplugin.util.PlayerTools.getOfflineOrOnlinePlayerByUuid;

/**
 * @author: geduo
 * @date: 2023/9/24 00:05
 */
public class InviteRejectExecutor {

    public static void reject(Player player, String[] args) {
        if (args.length != 2) {
            player.sendMessage(REJECT_COMMAND_FORMAT);
            return;
        }
        String uuid = player.getUniqueId().toString();
        TownPlayerDO townPlayer = TownPlayerManage.selectByUuid(uuid);
        if (townPlayer.getIdentity() == PlayerTownIdentity.MEMBER) {
            player.sendMessage(APPLY_ID_NOT_FOUND);
            return;
        }
        Integer applyId = null;
        try {
            applyId = Integer.valueOf(args[1]);
        } catch (NumberFormatException e) {
            player.sendMessage(REJECT_COMMAND_FORMAT);
            return;
        }
        if (townPlayer.getIdentity() == PlayerTownIdentity.LEADER) {
            TownApplyDO selectParam = new TownApplyDO();
            selectParam.setId(applyId);
            selectParam.setTownUuid(townPlayer.getTownUuid());
            selectParam.setStatus(ApplyStatus.APPLYING);
            List<TownApplyDO> townApplyList = TownApplyManage.selectList(selectParam);
            if (townApplyList.isEmpty()) {
                player.sendMessage(APPLY_ID_NOT_FOUND);
                return;
            } else {
                OfflinePlayer offlinePlayer = getOfflineOrOnlinePlayerByUuid(townApplyList.get(0).getPlayerUuid());
                TownApplyDO updateParam = new TownApplyDO();
                updateParam.setId(applyId);
                updateParam.setStatus(ApplyStatus.REFUSED);
                TownApplyManage.updateById(updateParam);
                player.sendMessage(String.format(LEADER_REJECT_APPLY_SUCCESS, offlinePlayer.getName()));
            }
        } else {
            TownApplyDO selectParam = new TownApplyDO();
            selectParam.setId(applyId);
            selectParam.setPlayerUuid(uuid);
            selectParam.setStatus(ApplyStatus.APPLYING);
            List<TownApplyDO> townApplyList = TownApplyManage.selectList(selectParam);
            if (townApplyList.isEmpty()) {
                player.sendMessage(APPLY_ID_NOT_FOUND);
                return;
            } else {
                TownApplyDO updateParam = new TownApplyDO();
                updateParam.setId(applyId);
                updateParam.setStatus(ApplyStatus.REFUSED);
                TownApplyManage.updateById(updateParam);
                player.sendMessage(String.format(PLAYER_REJECT_APPLY_SUCCESS, townApplyList.get(0).getTownName()));
            }
        }
    }
}
